<?php

class ectools_mdl_messenger_config extends desktop_controller {

    function __construct(&$app) {
        $this->app = $app;
        $this->columns = array(
            'sms_name' => array(
                'label' => app::get('ectools')->_('短信平台'),
                'width' => 200,
                'is_title' => true,
                'pkey' => true
            ),
            'sms_status' => array(
                'label' => app::get('ectools')->_('状态'),
                'width' => 100
            ),
            'sms_brief' => array(
                'label' => app::get('ectools')->_('通道简介'),
                'width' => 200
            )
        );
        
        $this->schema = array(
            'default_in_list' => array_keys($this->columns),
            'in_list' => array_keys($this->columns),
            'idColumn' => 'app_id',
            'textColumn' => 'app_display_name',
            'columns' => &$this->columns
        );
    }

    /**
     * 表名
     * @return string 表名
     */
    public function table_name() {
        return 'messenger_config';
    }

    function get_schema() {
        return $this->schema;
    }

    function count() {
        $smsConfServicelist = kernel::servicelist('ectools_messenger.ectools_mdl_messenger_config');
        return count($smsConfServicelist);
    }

    /**
     * 专为finder列表数据查询方法
     * @author Wangfy
     * @date 2019/07/19
     * @params $cols    查询字段
     * @params $filter  查询条件
     * @params $offset  偏移起始值
     * @params $limit   偏移位移值
     * @return $data    短信平台列表数据
     */
    public function getList($cols = '*', $filter = array(), $offset = 0, $limit = -1) {
        $smsList = $this->getSmsListInfo($filter, $offset, $limit);
        foreach ($smsList as $sms) {
            $object = $sms['object'];
            $class_name = $sms['class_name'];
            
            $row['sms_status'] = app::get('ectools')->_('关闭');
            if ($sms['sms_status'] == true) {
                $row['sms_status'] = app::get('ectools')->_('开启');
            }
            $row['sms_name'] = $object->name;
            $row['app_class'] = $class_name;
            $row['app_key'] = $object->app_key;
            
            // 获取短信平台简介
            $sms = new $sms['class_name']($this->app);
            $row['sms_brief'] = $sms->getSmsbrief();
            $data[] = $row;
            $start_index++;
        }
        return $data;
    }

    /**
     * 查询当前对接的短信平台信息
     * @author Wangfy
     * @date 2019/07/19
     * @params $cols    查询字段
     * @params $filter  查询条件
     * @params $offset  偏移起始值
     * @params $limit   偏移位移值
     * @return $data    短信平台列表数据
     */
    public function getSmsList($cols = '*', $filter = array(), $offset = 0, $limit = -1) {
        $smsList = $this->getSmsListInfo($filter, $offset, $limit);
        foreach ($smsList as $sms) {
            // 过滤查询字段逻辑
            if ($cols != '*') {
                $colArr = explode(",", $cols);
                foreach ($colArr as $col) {
                    $col = str_replace('`', '', $col);
                    if ($sms[$col]) {
                        $row[$col] = $sms[$col];
                    }
                }
                $sms = $row;
            }
            $data[] = $sms;
            $start_index++;
        }
        return $data;
    }

    /**
     * 短信平台信息共通查询（服务于finder查询和用户普通查询）
     * @author Wangfy
     * @date 2019/07/19
     * @params $filter  查询条件
     * @params $offset  偏移起始值
     * @params $limit   偏移位移值
     * @return $data    短信平台列表数据
     */
    private function getSmsListInfo($filter = array(), $offset = 0, $limit = -1) {
        // 获取当前已对接的短信平台Service
        $smsConfServicelist = kernel::servicelist('ectools_messenger.ectools_mdl_messenger_config');
        $startIndex = 0;
        foreach ($smsConfServicelist as $class_name => $object) {
            // 当前循环索引<起始值 或 当前循环索引超过查询总区间 跳过本次循环
            if ($startIndex < $offset || ($limit > 0 && $startIndex >= ($offset + $limit))) {
                continue;
            }
            $messengerStr = $this->app->getConf($class_name);
            $messengerArr = unserialize($messengerStr);
            $messengerArr = $messengerArr['setting'];
            
            // 默认符合条件
            $filterFlg = true;
            // 筛选条件逻辑
            if (is_array($filter)) {
                foreach ($filter as $fKey => $fValue) {
                    if ($messengerArr[$fKey] == $fValue) {
                        continue;
                    } else {
                        // 有一个条件未满足则不符合条件，结束循环
                        $filterFlg = false;
                        break;
                    }
                }
            }
            // 不符合条件的跳过当前循环
            if (! $filterFlg) {
                continue;
            }
            $messengerArr['app_key'] = $object->app_key;
            $messengerArr['object'] = $object;
            $messengerArr['class_name'] = $class_name;
            $data[] = $messengerArr;
            $start_index++;
        }
        return $data;
    }
}
?>
